#!/bin/bash

# quick latex writer test runner
# $Id$

echo "THIS is not updated in normal commits"

CHECKS=0
FAILS=0
FAIL_QUEUE=""

function do_diff() {
  CHECKS=$((CHECKS + 1)) 
  diff -q -s $1 $2
  if test $? -gt 0 ; then
    FAILS=$((FAILS + 1)) 
    FAIL_QUEUE="$FAIL_QUEUE,$1"
  fi
}

function report() {
  echo "-------------------------------------"
  if test $FAILS -eq 0 ; then
    echo "OK: all $CHECKS tests passed"
  else
    echo "ERROR: $FAILS of $CHECKS failed"
    echo "Failed tests: $FAIL_QUEUE"
  fi
}

BARE_CMD="../../docutils/tools/rst2latex.py --traceback "
BASE_CMD="$BARE_CMD --stylesheet= "

CMD="$BASE_CMD"
if [ -z "$1" ] ; then
  FM="*.txt"
else
  FM=$1
fi

for INF in `ls test/$FM` ; do
  OUTF="$INF.tex"
  REFF="$OUTF.ref"
  OPT=""
  if [ $INF = "test/unicode-box.txt" ] ; then
    OPT="-o utf-8:strict"
  fi
  $CMD $OPT $2 $3 $INF > $OUTF
  do_diff $REFF $OUTF 
done

if [ -n "$1" -a ! "$1" == "-" ] ; then
  report
  exit
fi

# stylesheet embedding
#
# fail for no extension
INF=test/header.txt
STF=test/dummy_stylesheet
echo -e "% dummy stylesheet\n%  second line" > $STF
OUTF="$INF-embed-stylesheet.err"
REFF="$OUTF.ref"
$CMD --stylesheet-path=$STF --embed-stylesheet $2 $3 $INF > $OUTF 2>&1
do_diff $REFF $OUTF
# embed for .inc
mv $STF $STF.inc
OUTF="$INF-embed-stylesheet.tex"
REFF="$OUTF.ref"
$CMD --stylesheet-path=$STF.inc --embed-stylesheet $2 $3 $INF > $OUTF 2>&1
do_diff $REFF $OUTF
# embed for .sty
mv $STF.inc $STF.sty
OUTF="$INF-embed-stylesheet-sty.tex"
REFF="$OUTF.ref"
$CMD --stylesheet-path=$STF.sty --embed-stylesheet $2 $3 $INF > $OUTF 2>&1
do_diff $REFF $OUTF
exit
INF=test/header.txt
for GO in "" auto pdftex dvips,300dpi ; do
    OUTF="$INF-graphicx-option-$GO.tex"
    REFF="$OUTF.ref"
    $CMD --graphicx-option=$GO $2 $3 $INF > $OUTF
    do_diff $REFF $OUTF 
done

# font encoding
for FONTENC in "" T1 OT1 ; do
  for INF in `ls test/characters.txt` ; do
    OUTF="$INF-$FONTENC.tex"
    REFF="$OUTF.ref"
    if [ -z $FONTENC ] ; then
      $CMD $2 $3 $INF > $OUTF
    else
      $CMD --font-encoding=$FONTENC $2 $3 $INF > $OUTF
    fi
    do_diff $REFF $OUTF 
  done
done

# reference-label
for LR in "" "ref" ; do
  for INF in test/links.txt ; do
    OUTF="$INF-$LR.tex"
    REFF="$OUTF.ref"
    $CMD --reference-label=$LR $2 $3 $INF > $OUTF
    do_diff $REFF $OUTF 
  done
done

INF="test/tables.txt"
OUTF="$INF-verbatim.tex"
REFF="$OUTF.ref"
$CMD --use-verbatim-when-possible $2 $3 $INF > $OUTF
do_diff $REFF $OUTF 

INF="test/tables.txt"
OUTF="$INF-literal-block-env.tex"
REFF="$OUTF.ref"
$CMD --literal-block-env="lstlisting[language=Python]" $2 $3 $INF > $OUTF
do_diff $REFF $OUTF 

# table-style
for TABSTYLE in "" standard booktabs nolines ; do
  for INF in `ls test/tables.txt` ; do
    OUTF="$INF-$TABSTYLE.tex"
    REFF="$OUTF.ref"
    if [ -z $TABSTYLE ] ; then
      $CMD $2 $3 $INF > $OUTF
    else
      $CMD --table-style=$TABSTYLE $2 $3 $INF > $OUTF
    fi
    do_diff $REFF $OUTF 
  done
done

INF=test/sections.txt
OUTF="$INF-parts.tex"
REFF="$OUTF.ref"
$CMD --use-part-section $2 $3 $INF > $OUTF
do_diff $REFF $OUTF 

# rowspan tables
INF=test/tables-multirow.txt
OUTF="$INF.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

# with generated footer
INF=test/docinfo.txt
OUTF="$INF-gs.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

CMD="$BASE_CMD --documentclass=book"
INF=test/docinfo.txt
OUTF="$INF-book.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

CMD="$BASE_CMD --documentclass=custom"
INF=test/docinfo.txt
OUTF="$INF-custom.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

CMD="$BASE_CMD --no-doc-info"
INF=test/docinfo.txt
OUTF="$INF-no-info.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

CMD="$BASE_CMD --no-doc-title"
INF=test/docinfo.txt
OUTF="$INF-no-title.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

CMD="$BASE_CMD --no-doc-title --no-doc-info"
INF=test/docinfo.txt
OUTF="$INF-no-title-info.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

CMD="$BASE_CMD --use-titlepage-env"
for F in docinfo docinfo-1 docinfo-2 docinfo-3 ; do
    INF=test/$F.txt
    OUTF="$INF-use-titlepage-env.tex"
    REFF="$OUTF.ref"
    $CMD $INF > $OUTF
    do_diff $REFF $OUTF 
done

CMD="$BASE_CMD --use-latex-abstract --use-titlepage-env"
for F in docinfo-2 docinfo-3 ; do
    INF=test/$F.txt
    OUTF="$INF-use-titlepage-env-latexabstract.tex"
    REFF="$OUTF.ref"
    $CMD $INF > $OUTF
    do_diff $REFF $OUTF 
done

CMD="$BASE_CMD --use-latex-abstract"
F=docinfo-2
INF=test/$F.txt
OUTF="$INF-use-latex-abstract.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

CMD="$BASE_CMD --use-latex-docinfo"
for F in docinfo docinfo-2 ; do
  INF=test/$F.txt
  OUTF="$INF-use-latex-docinfo.tex"
  REFF="$OUTF.ref"
  $CMD $INF > $OUTF
  do_diff $REFF $OUTF 
done

CMD="$BASE_CMD"
# with latex toc
for F in toc toc-autonr ; do
  INF=test/$F.txt
  OUTF="$INF-use-latex-toc.tex"
  REFF="$OUTF.ref"
  $CMD --use-latex-toc $INF > $OUTF
  do_diff $REFF $OUTF 
done

# german
for F in toc quotes ; do
  for L in de fr it ; do
    INF=test/$F.txt
    OUTF="$INF-$L.tex"
    REFF="$OUTF.ref"
    $CMD -l $L $INF > $OUTF
    do_diff $REFF $OUTF 
  done
done

# spanish --language=es flags inhibits --use-latex-docinfo when using
# --documentclass=book and --use-latex-toc
CMD="$BASE_CMD --documentclass=book --use-latex-toc --use-latex-docinfo"
for F in toc quotes ; do
  INF=test/$F.txt
  OUTF="$INF-latex-docinfo.tex"
  REFF="$OUTF.ref"
  $CMD $INF > $OUTF
  LANG=es
  do_diff $REFF $OUTF 
  OUTF="$INF-$LANG-latex-docinfo.tex"
  REFF="$OUTF.ref"
  $CMD --language=$LANG $INF > $OUTF
  do_diff $REFF $OUTF 
done

# use latex footnotes
CMD="$BASE_CMD --use-latex-footnotes"
INF=test/footnotes.txt
OUTF="$INF-use-latex-footnotes.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

# use latex citations
CMD="$BASE_CMD --use-latex-citations"
for F in footnotes.txt no-citations.txt citation_labels.txt ; do
  INF=test/$F
  OUTF="$INF-use-latex-citations.tex"
  REFF="$OUTF.ref"
  $CMD $INF > $OUTF
  do_diff $REFF $OUTF 
done

# use bibtex
INF=test/citations.txt
CMD="$BASE_CMD --use-latex-citations --use-bibtex=mystyle,mydb1,mydb2"
OUTF="$INF-use-bibtex.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

CMD="$BASE_CMD --use-verbatim-when-possible"
INF=test/literal.txt
OUTF="$INF-use-verbatim-when-possible.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

for L in en de es ; do
    CMD="$BASE_CMD "
    INF=test/literal.txt
    OUTF="$INF-$L.tex"
    REFF="$OUTF.ref"
    $CMD --lang=$L $INF > $OUTF
    do_diff $REFF $OUTF 
    OUTF="$INF-$L-verb.tex"
    REFF="$OUTF.ref"
    $CMD --use-verbatim-when-possible --language=$L $INF > $OUTF
    do_diff $REFF $OUTF 
done

report
echo "CHECK: toc with latex toc "

exit

# lost test files.

INF=test/kasten.txt
OUTF="$INF-de.tex"
REFF="$OUTF.ref"
$CMD -l de $INF > $OUTF
do_diff $REFF $OUTF 

CMD="$BASE_CMD --attribution=parens"
INF=test/attribution.txt
OUTF="$INF-parens-attribution.tex"
REFF="$OUTF.ref"
$CMD $INF > $OUTF
do_diff $REFF $OUTF 

INF=test/iso-8859-15.txt
OUTF="$INF-enc.tex"
REFF="$OUTF.ref"
$CMD --input-encoding=iso-8859-15 --output-encoding=iso-8859-15 $INF > $OUTF
do_diff $REFF $OUTF 


